1316B - String Modification - CodeForces Solution


brute force constructive algorithms implementation sortings strings *1400

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    n = int(input())
    s = input()
    best = min(s)



    indexes = []
    for i, item in enumerate(s):
        if item == best:
            indexes.append(i)
    


    best_string = 'z'*10
    k = None
    for item in indexes:
                        
            
            
            
            
        
        
        
        string = s[item:]
        if (n-item)%2 == 0:string+=s[:item]
        else:string += s[:item][::-1]
        
        
        
        
        
        if "".join(string) < best_string:
            best_string = "".join(string)
            k = item
    
    
    print(best_string);print(k+1)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
ll mod = 1e9+7;
 
 
// cout<<setprecision(10)<<fixed<<ans<<endl; for double



//sieve of earonsthes
// const int N = 1e7;
// bool prime[N+1];
// void sieve()
// {
//     memset(prime, true, sizeof(prime));
//     prime[0] = false;
//     prime[1] = false;
//     for(ll i = 2; i*i <= N; i++)
//     {
//         if(prime[i] == true)
//         {
//             for(ll j = i*i; j <= N; j += i)
//             {
//                 prime[j] = false;
//             }
//         }
//     }
// }
 
 
// making multiples of a number as false.
// void sieve()
// {
//     memset(prime, true, sizeof(prime));
//     prime[0] = false;
//     for(auto it : mpp)
//     {
//         if(prime[it.first] == true)
//         {
//             prime[it.first] = false;
//             for(ll j = it.first*2; j <= N; j += it.first)
//             {
//                 prime[j] = false;
//             }
//         }
//     }
// }
 

// sum of digits
// string sum_of_digits(string str)
// {
//     ll sum = 0;
//     for(ll i = 0; i < str.length(); i++)
//     {
//         sum += (str[i] - '0');
//     }
    
//     string ans = to_string(sum);
//     return ans;
// }

// count of numbers divisible by m, in the range a to b
// ll count_divisible(ll a, ll b, ll m)
// {
//     if(a % m == 0)
//     {
//         return (b/m) - (a/m) + 1;
//     }
//     else
//     {
//         return (b / m) - (a/m);
//     }
// }

// storing prime factors

// ll facts(ll n)
// {
//     ll cnt = 0;
//     for(ll d = 2; d * d <= n; d++) {
//         while(n % d == 0)
//         {
//             if(d % 2 == 0)cnt++;
//             n /= d;
//         }
//     }
//     if(n > 1 && n % 2 == 0) cnt++;
//     return cnt;
// }



signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    ll t;
    cin >> t;
    //sieve();
    while(t--)
    {
        // freopen("input.txt", "r", stdin);
        // freopen("output.txt", "w", stdout);
        
        ll n;
        cin >> n;
        string str;
        cin >> str;
        
        vector<ll> pos;
        char mini = 'z';
        for(ll i = 0; i < str.length(); i++)
        {
            mini = min(mini, str[i]);
        }
        
        for(ll i = 0; i < str.length(); i++)
        {
            if(str[i] == mini)
            {
                pos.push_back(i+1);
            }
        }
        
        
        //for(auto it : pos) cout<<it<<" ";
        
        ll mini_ans = 1e9;
        string mini_string = "";
        for(ll i = 1; i <= 5001; i++)
        {
            mini_string += 'z';
        }
        for(ll i = 0; i < pos.size(); i++)
        {
            ll val = pos[i] - 1;
            string ans = "";
            ans += str.substr(val, n);
            if(n % 2 != val % 2)
            {
                string temp = "";
                temp = str.substr(0, val);
                reverse(temp.begin(), temp.end());
                ans += temp;
            }
            else
            {
                ans += str.substr(0, val);
            }
            

            if(ans < mini_string)
            {
                mini_string = ans;
                mini_ans = pos[i];
            } 
        }
        if(mini_ans == 1e9)
        {
            cout<<str<<endl;
            cout<<"1"<<endl;
        }
        else
        {
        cout<<mini_string<<endl;
        cout<<mini_ans<<endl;
        }
    }
}


Comments

Submit
0 Comments
More Questions

6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold
1268. Search Suggestions System
841. Keys and Rooms
152. Maximum Product Subarray
337. House Robber III
869. Reordered Power of 2
1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String
72. Edit Distance
563. Binary Tree Tilt
1306. Jump Game III
236. Lowest Common Ancestor of a Binary Tree
790. Domino and Tromino Tiling
878. Nth Magical Number
2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram